
package Loader;

import Settings.Settings;
import java.util.Iterator;
import java.util.LinkedList;

public class BufferedData {
    
    public LinkedList<Pair> data;
    public LinkedList<Pair> dataLearn;
    public LinkedList<Pair> dataTest;
    public float[] minValues;
    public float[] maxValues;
    public float[] granularity;
    public int dataSize;

    public BufferedData(LinkedList<Pair> data) {
        
        this.data = data;
        

        if(data == null)
            return;

        this.dataSize = data.size();

        float[] temp = data.getFirst().features;

        this.minValues = new float[ temp.length ];
        this.maxValues = new float[ temp.length ];
        this.granularity = new float[ temp.length ];

        for(int i = 0; i < minValues.length; ++i) {
            minValues[ i ] = temp[ i ];
            maxValues[ i ] = temp[ i ];
        }

        for (Iterator<Pair> it = data.iterator(); it.hasNext();) {

            Pair pair = it.next();

            for(int i = 0; i < minValues.length; ++i) {

                if(minValues[ i ] > pair.features[ i ])
                    minValues[ i ] = pair.features[ i ];

                if(maxValues[ i ] < pair.features[ i ])
                    maxValues[ i ] = pair.features[ i ];

            }
        }

        int bits = Settings.CHROMOSOME_NUMBER_OF_BITS_PER_VALUE;
        int maxValue = (int) Math.pow( 2, bits );
        for(int i = 0; i < minValues.length; ++i)
            granularity[i] = ( maxValues[i] - minValues[i] ) /
                             maxValue;

        divideDataSet();

    }

    public void shuffleData(int repeat) {

        if(data == null)
            return;

        for(int r = 0; r < repeat; ++r) {

            int pos1 = (int) ( Math.random() * data.size() );
            int pos2 = (int) ( Math.random() * data.size() );
            Pair p1 = data.get(pos1);
            Pair p2 = data.get(pos2);
            data.set(pos2, p1);
            data.set(pos1, p2);
            
        }

        divideDataSet();
        
    }

    private void divideDataSet() {

        int percent = Settings.PERCENT_DATA_LEARN;
        int cutIndex = ( dataSize * percent ) / 100;

        dataLearn = new LinkedList<Pair>( data.subList( 0, cutIndex ) );

        dataTest = new LinkedList<Pair>( data.subList( cutIndex, dataSize ) );

    }


}
